package chaos.util;

import java.io.InputStream;

public class SmartResourceManager implements ResourceManager {
	CMPQResourceManager cMan;
	MPQResourceManager rpqMan;
	ZIPResourceManager zipMan;
	AbsPathResourceManager pathMan;
	
	public SmartResourceManager(MPQResourceManager rpqMan,	ZIPResourceManager zipMan, AbsPathResourceManager pathMan) {
		this.rpqMan = rpqMan;
		this.zipMan = zipMan;
		this.pathMan = pathMan;
	}
	
	public void setCMPQManager(CMPQResourceManager cman) {
		this.cMan = cman;
	}

	public void init() throws Exception {
	}

	public void close() {
		try {
			if(rpqMan != null) rpqMan.close();
		} catch (Exception e) {
		}
		try {
			if(zipMan != null) zipMan.close();
		} catch (Exception e) {
		}
		try {
			if(pathMan != null) pathMan.close();
		} catch (Exception e) {
		}
	}
	
	public InputStream getInputStream(String path) {
		InputStream find = null;
		if(cMan != null) find = cMan.getInputStream(path);
		
		if(find == null && zipMan != null) find = zipMan.getInputStream(path);
		
		if(find == null && rpqMan != null) find = rpqMan.getInputStream(path);
		
		if(find == null && pathMan != null) find = pathMan.getInputStream(path);
		return find;
	}
}